package top.javai.www.quartz;


import org.quartz.Scheduler;
import org.quartz.ee.servlet.QuartzInitializerListener;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import java.io.IOException;
import java.util.Properties;


@Configuration
public class SchedulerConfig {

    private JobFactory jobFactory;

    public SchedulerConfig(JobFactory jobFactory){
        this.jobFactory = jobFactory;
    }

    /**
     * 调度器工厂对象,并设置quartz属性
     * @param quartzProperties
     * @return
     * @throws IOException
     */
    @Bean(name = "SchedulerFactory")
    public SchedulerFactoryBean schedulerFactoryBean(@Qualifier("quartzProperties")
                                                     Properties quartzProperties) throws IOException{
        SchedulerFactoryBean factory = new SchedulerFactoryBean();
        factory.setQuartzProperties(quartzProperties);
        factory.setJobFactory(jobFactory);
        return factory;
    }

    /**
     * 加载quartz的配置
     * @return
     * @throws IOException
     */
    @Bean(name = "quartzProperties")
    public Properties quartzProperties() throws IOException{
        PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
        propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
        //在quartz.properties中的属性被读取并注入后再初始化对象
        propertiesFactoryBean.afterPropertiesSet();
        return propertiesFactoryBean.getObject();
    }

    /**
     * quartz初始化监听器
     * @return
     */
    @Bean
    public QuartzInitializerListener executorListener(){
        return new QuartzInitializerListener();
    }

    /**
     * 通过SchedulerFactoryBean获取Scheduler 调度器的实例
     * @param schedulerFactoryBean
     * @return
     * @throws IOException
     */
    @Bean(name = "Scheduler")
    public Scheduler scheduler(SchedulerFactoryBean schedulerFactoryBean) throws IOException{
        return schedulerFactoryBean.getScheduler();
    }
}
